package com.res.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.res.entity.Subjects;
import com.res.service.SubjectsService;
import com.res.util.FenyeUtil;

@RestController
@RequestMapping("/subjects")
public class SubjectsController {
@Autowired
private SubjectsService subjectsService;
@GetMapping
public FenyeUtil<Subjects>findByName(Subjects s,Integer page){
	FenyeUtil<Subjects>f=new FenyeUtil<Subjects>();
	Pageable pageable=PageRequest.of(page-1, 10);
	Page<Subjects>ps=subjectsService.findByMoHu(s, pageable);
	int count=(int)ps.getTotalElements();
	f.setCount(count);
	f.setData(ps.getContent());
	f.setCode(0);
	return f;
}
@GetMapping("/sub")
public FenyeUtil<Subjects> getAllSub() {
	FenyeUtil<Subjects> f=new FenyeUtil<Subjects>();
	 f.setData(subjectsService.getAllSub());	
	return f;
}
@PostMapping
public Object AddSub(Subjects s) {
	try {
		subjectsService.AddAndUpdateBySub(s);
		return 1;
	} catch (Exception e) {
		// TODO: handle exception
		return 0;
	}
}
@PutMapping
public Object UpSub(Subjects s) {
	try {
		subjectsService.AddAndUpdateBySub(s);
		return 1;
	} catch (Exception e) {
		// TODO: handle exception
		return 0;
	}
}
@DeleteMapping
public Integer DelteSub(Integer sid) {
	try {
		subjectsService.deleteSub(sid);
		return 1;
	} catch (Exception e) {
		// TODO: handle exception
		return 0;
	}
}
}
